package com.infovane.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import com.infovane.dao.ArticleDao;
import com.infovane.dao.BaseDao;
import com.infovane.model.TArticle;
import com.infovane.service.ArticleService;

public class ArticleServiceImpl implements ArticleService{
	
	
	private BaseDao baseDao;
	
	
	
	public BaseDao getBaseDao() {
		return baseDao;
	}
	

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	
	private ArticleDao articleDao;
	
	
	

	public ArticleDao getArticleDao() {
		return articleDao;
	}


	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}


	/**
	 * 分页获取文章数据
	 * @param first,count
	 * @return List
	 * */
	public List getArticleList(int currentPage, int pageSize) {
		String hql="from TArticle as a where a.articleIfConsult='"+1+"' order by a.articleCreateTime desc";
		return this.baseDao.load(hql, (currentPage-1)*pageSize, pageSize);
	}
	
	
	/**
	 * 获取数据库中文章数据记录数
	 * @return int
	 */
	public int getCount() {
		int count=0;
		String hql="select a.articleId from TArticle as a";
		List list=this.baseDao.load(hql);
		if(list.size()>0){
			count=list.size();	
		}
		return count;
	}
	
	/**
	 * 添加文章对象
	 * @param Object
	 * @return boolean
	 * */
	public boolean add(Object object) {
		
		return this.baseDao.save(object);
	}
	/**
	 * 根据文章ID加载文章对象
	 * @param classModel,id
	 * @return Object
	 * */
	public Object get(Class modelClass, Long id) {
		
		return this.baseDao.load(modelClass, id);
	}
	
	/**
	 * 更新（修改）文章对象
	 * @param Objcet
	 * @return boolean
	 * */
	public boolean update(Object object) {
		
		return this.baseDao.update(object);
	}
	
	/**
	 * 获取设置了标题颜色的文章
	 * @param color, consult,authority
	 * @return List
	 * */
	public List getColor(String color,int consult,int authority) {
		String hql="from TArticle as a where a.articleTitleColor='"+color+"' and a.articleIfConsult='"+consult+"' and a.articleIfAuthority='"+authority+"' order by a.articleCreateTime desc";
		return this.baseDao.load(hql);
	}
	
	/**
	 * 获取官方推荐文章
	 * @return List
	 * */
	public List getGrade1() {
		String hql="from TArticle as a where a.articleGrade='"+1+"' order by a.articleCreateTime desc";
		return this.baseDao.load(hql, 0, 4);
	}
	
	/**
	 * 根据关键字查询文章
	 * @param keywords
	 * @return List
	 * */
	public List getQuery(String keywords) {
		String hql="from TArticle as a where a.articleTitle like ? or a.articleKeywords like ? or a.articleSummary like ? or a.articleTypeName like ?" +
				"or a.articleStyleName like ? or a.authorName like ? or a.articleContent like ? order by a.articleCreateTime desc";
		return this.articleDao.query(hql, keywords);
	}

	/**
	 * 获取热门文章
	 * @return List
	 * */
	public List<TArticle> getClickArticle() {
		String hql="select a.articleId,a.articleTitle from TArticle as a order by a.articleClick desc";
		List list =  this.baseDao.load(hql, 0, 15);
		Iterator it=list.iterator();
		List<TArticle> articleList=new ArrayList<TArticle>();
		while(it.hasNext()){
			TArticle article=new TArticle();
			Object[] results=(Object[]) it.next();
			article.setArticleId((Long)results[0]);
			article.setArticleTitle((String)results[1]);
			articleList.add(article);
		}
		return articleList;
	}
	
	
	
	/**
	 * 获取评论最多文章
	 * @return List
	 * */
	public List<TArticle> getSumComment() {
		String hql="select a.articleId,a.articleTitle from TArticle as a order by a.articleSumComment desc";
		List list=this.baseDao.load(hql, 0, 10);
		Iterator it=list.iterator();
		List<TArticle> articleList=new ArrayList<TArticle>();
		while(it.hasNext()){
			TArticle article=new TArticle();
			Object[] results=(Object[]) it.next();
			article.setArticleId((Long)results[0]);
			article.setArticleTitle((String)results[1]);
			articleList.add(article);
		}
		return articleList;
	}


	/**
	 * 根据传入的参数获取相关文章数
	 * @param username,attentioneType
	 * @return int
	 */
	public int getCount(String username,String attentioneType) {
		int count=0;
		StringBuffer hql=new StringBuffer();
		hql.append("select a.articleId from TArticle as a");
		if(username!=null&&attentioneType==null){
			hql.append(" where a.authorName='"+username+"'");
			
		}
		if(attentioneType!=null&&username==null){
			hql.append(" where a.articleTypeName='"+attentioneType+"'");
		}
		List list=this.baseDao.load(hql.toString());
		if(list.size()>0){
			count=list.size();
		}
		return count;
	}

	/**
	 * 根据传入参数获取资讯
	 * @param first,count,username,attentioneType
	 * @return List
	 * */
	public List getUserArticleList(int currentPage, int pageSize,
			String username,String attentioneType) {
		StringBuffer hql=new StringBuffer();
		hql.append("from TArticle as a");
		if(username!=null&&attentioneType==null){
			hql.append(" where a.authorName='"+username+"'");
		}
		if(username==null&&attentioneType!=null){
			hql.append(" where a.articleTypeName='"+attentioneType+"'");
		}
		return this.baseDao.load(hql.toString(),(currentPage-1)*pageSize, pageSize);
	}

}
